﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Jy123.Web.AppCode;

namespace Jy123.Web.Letter
{
    public partial class Notice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;//暂时
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            List<int?> count;
            StringBuilder sbHtml = new StringBuilder();
            sbHtml = CommonHtml(sbHtml, "user_letter_notice");//公共部分的html
            sbHtml.Replace("$NoticeList$",LetterList(userID,out count));
            sbHtml.Replace("$NoticeCount$",count[0].ToString());
            sbHtml.Replace("$ClassIn$",  string.Empty)
            .Replace("$ClassOut$", string.Empty)
            .Replace("$ClassNotice$","class=\"sel\"")
            .Replace("$ClassAdd$", string.Empty);
            Response.Write(sbHtml.ToString());
        }
        /// 公共部分的html
        /// </summary>
        /// <param name="sbHtml">最终写出页面的大StringBuilder</param>
        /// <param name="cacheHtml">缓存名称</param>
        /// <returns>StringBuilder</returns>
        private StringBuilder CommonHtml(StringBuilder sbHtml, string cacheHtml)
        {
            sbHtml = new StringBuilder(Cache[cacheHtml].ToString());
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            return sbHtml;
        }

        /// <summary>
        /// 站内信列表
        /// </summary>
        /// <param name="userID">当前用户ID</param>
        /// <returns>字符串</returns>
        private string LetterList(int userID,out List<int?> count)
        {
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = Cache["user_letter_noticelist"].ToString();
            DataTable dtList = new Jy123.BLL.User.Letter().LetterList(currentpage, pagesize, "LtType=0", "PostTime DESC", out count).Tables[0];
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                string str = strHtml;
                str = str.Replace("$LetterID$", dtList.Rows[i]["ID"].ToString());
                //str = str.Replace("$UserID$", dtList.Rows[i]["FUserSender"].ToString());
                //str = str.Replace("$UserName$", dtList.Rows[i]["SenderName"].ToString());
                //str = str.Replace("$Portrait$", dtList.Rows[i]["SenderPortrait"].ToString());
                str = str.Replace("$Title$", dtList.Rows[i]["Title"].ToString());
                str = str.Replace("$NoticeContent$",Jy123.BLL.User.UserSql.BlogContent(dtList.Rows[i]["LetterContent"].ToString()));
                str = str.Replace("$PostTime$", dtList.Rows[i]["PostTime"].ToString());
                //str = str.Replace("$LetterCommentList$", LetterCommentList(Convert.ToInt32(dtList.Rows[i]["ID"]), userID, out countComment));
                //str = str.Replace("$CommentCount$", countComment[0].ToString());
                //string isRead = dtList.Rows[i]["IsRead"].ToString();
                //if (isRead == "1")//如果是已经阅读过的
                //{ str = str.Replace("<b class=\"red\">[未阅读]</b>", "<B class=blue>[已阅]</B>"); }
                sbHtml.Append(str);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "Notice.aspx?page=$num$";
            if (count[1] > 1)
            {
                MoonPage mp = new MoonPage(false, true);
                mp.Count = Convert.ToInt32(count[1]);
                sbHtml.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            }
            return sbHtml.ToString();
        }
    }
}
